#include "path_stack.h"

int pathStackInit(pathStack_t *ppathStack)
{
    memset(ppathStack, 0, sizeof(pathStack_t));
    return 0;
}

int pathStackPush(pathStack_t *ppathStack,char *dirName){
    dirNode_t *pNewNode = (dirNode_t *)calloc(1, sizeof(dirNode_t));
    if(pNewNode == NULL){
        printf("calloc申请空间失败!\n");
        return -1;
    }
    strncpy(pNewNode->dirName, dirName, sizeof(pNewNode->dirName) - 1);
    pNewNode->dirName[sizeof(pNewNode->dirName) - 1] = '\0';
    if (ppathStack->stackSize == 0){
        ppathStack->first = pNewNode;
        ppathStack->last = pNewNode;
    } 
    else {
        ppathStack->last->next = pNewNode;
        pNewNode->pre = ppathStack->last;
        ppathStack->last = pNewNode;
    }
    ppathStack->stackSize++;
    return 0;
}

int pathStackPop(pathStack_t *ppathStack)
{
    if(ppathStack->first == NULL){
        printf("已在根目录！\n");
        return 0;
    }
    dirNode_t *curNode = ppathStack->last;

    if(ppathStack->stackSize == 1)
    {
        ppathStack->first = NULL;
        ppathStack->last = NULL;
    }
    else
    {
        ppathStack->last = ppathStack->last->pre;
        ppathStack->last->next = NULL;
    }
    ppathStack->stackSize--;
    free(curNode);
    return 0;
}

int pathStackPrin(pathStack_t *ppathStack){
    dirNode_t *q = ppathStack->first;
    while(q)
    {
        printf("%s",q->dirName);
        q=q->next;
        if(q->next != NULL){
            printf("-->");
        }
    }
    printf("\n");
    return 0;
}

int stack_output_path(pathStack_t *pathStack,char *path){
    path[0] = '/';
    path[1] = '\0';
    //保证绝对路径 
    dirNode_t *node = pathStack->first;
    while (node) {
        // 检查缓冲区空间
        size_t cur_len = strlen(path);
        size_t name_len = strlen(node->dirName);

        if (cur_len + name_len + 1 >= 1024) {
            printf("路径过长!\n");
            return -1;
        }
        strcat(path, node->dirName);
        // 如果不是最后一个节点，添加分隔符
        if (node->next) {
            strcat(path, "/");
        }
        node = node->next;
    }
    printf("sPrintf中的路径: %s\n", path);//debug用
    return 0;
}

int copyStack(pathStack_t *Sstack,pathStack_t *Dstack){
    
    pathStackInit(Dstack);

    if (Sstack->stackSize == 0) { //应该不会执行
        return 0;
    }

    dirNode_t *current = Sstack->first;
    while (current != NULL) {
        dirNode_t *newNode = (dirNode_t *)calloc(1, sizeof(dirNode_t));
        if (!newNode) {
            perror("calloc内存分配失败");
            return -1;
        }

        //复制名称
        strncpy(newNode->dirName, current->dirName, sizeof(newNode->dirName) - 1);
        newNode->dirName[sizeof(newNode->dirName) - 1] = '\0';


        // 初始化指针
        newNode->next = NULL;
        newNode->pre = Dstack->last;

        // 链接到链表
        if (Dstack->last == NULL) {
            Dstack->first = newNode;
        } else {
            Dstack->last->next = newNode;
        }

        Dstack->last = newNode;
        ++Dstack->stackSize;
        
        current = current->next;
    }

    // 看一下有没有问题 debug
    
    char a[1024], b[1024];
    stack_output_path(Dstack, a);
    printf("复制后的栈中路径: %s\n", a);
    stack_output_path(Sstack, b);
    printf("原来栈中路径: %s\n", b);

    return 0;
}